import { createRouter, createWebHistory } from 'vue-router'
import { ElMessageBox } from 'element-plus'

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/home',
    component: () => import('../views/Home.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '/home',
        name: 'HomePage',
        component: () => import('../views/home/index.vue')
      },
      // 流程管理相关路由
      {
        path: '/workflow',
        name: 'Workflow',
        component: () => import('../views/workflow/index.vue'),
        meta: { title: '流程管理' },
        children: [
          {
            path: 'process-model',
            name: 'ProcessModel',
            component: () => import('../views/workflow/processesManage/process-model.vue'),
            meta: { title: '流程模型管理' }
          },
          {
            path: 'process-definition',
            name: 'ProcessDefinition',
            component: () => import('../views/workflow/processesManage/process-definition.vue'),
            meta: { title: '流程定义管理' }
          },
          {
            path: 'form-config',
            name: 'FormConfig',
            component: () => import('../views/workflow/processesManage/form-config.vue'),
            meta: { title: '表单配置' }
          },
          {
            path: 'form-design',
            name: 'FormDesign',
            component: () => import('../views/workflow/processesManage/form-design.vue'),
            meta: { title: '表单设计' }
          },
          {
            path: 'process-definition/:id',
            name: 'ProcessDefinitionDetail',
            component: () => import('../views/workflow/processesManage/process-definition-detail.vue'),
            meta: { title: '流程定义详情' }
          },
          {
            path: 'processesManage/editor',
            name: 'ProcessDefinitionEditor',
            component: () => import('../views/workflow/processesManage/process-definition-editor.vue'),
            meta: { title: '流程定义编辑器' }
          }
        ]
      },//任务路由
      {
        path: '/task',
        name: 'task',
        component: () => import('../views/workflow/task.vue'),
        meta: { title: '任务管理' },
        children: [
          {
            path: 'originating-task',
            name: 'OriginatingTask',
            component: () => import('../views/workflow/taskManage/originating-task.vue'),
            meta: { title: '发起任务' }
          },
          
        ]
      },
      // 其他现有路由
      {
        path: '/user/msg',
        name: 'UserMsg',
        component: () => import('../views/user/user-msg.vue')
      },
      {
        path: '/user/profile',
        name: 'UserProfile',
        component: () => import('../views/user/Profile.vue')
      },
      {
        path: '/change-password',
        name: 'ChangePassword',
        component: () => import('../views/ChangePassword.vue')
      },
      {
        path: '/userroleGroup',
        name: 'UserRoleGroup',
        component: () => import('../views/userRoleGroup/userRoleGroup.vue')
      },
      {
        path: '/permission/bind-permission',
        name: 'BindPermission',
        component: () => import('../views/permission/bind-permission.vue')
      },
      {
        path: '/rayfile',
        name: 'RayFile',
        component: () => import('../views/rayfile/index.vue')
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 检查路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!token) {
      // 如果没有token，显示确认对话框
      ElMessageBox.confirm(
        '登录已过期，是否重新登录？',
        '提示',
        {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(() => {
          next({
            path: '/login',
            query: { redirect: to.fullPath }
          })
        })
        .catch(() => {
          next(false) // 取消导航
        })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
